package com.itlong.cloud.operate.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.itlong.cloud.POJO.DTO.operate.*;
import com.itlong.cloud.POJO.PO.DeveloperAccountAuthPO;
import com.itlong.cloud.POJO.PO.PropertyRoleProjectPO;
import com.itlong.cloud.POJO.PO.PropertyUserInfoPO;
import com.itlong.cloud.POJO.PO.RobotDeveloperAccountAuthPO;
import com.itlong.cloud.POJO.VO.operate.*;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.constants.RedisConstant;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.enumerate.PropertyPublicRoleEnum;
import com.itlong.cloud.operate.dao.IOperateDeveloperAuthDao;
import com.itlong.cloud.operate.dao.IOperatePropertyAccountDao;
import com.itlong.cloud.operate.service.IOperateDeveloperAuthService;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.date.DateUtil;
import com.itlong.cloud.utils.encrypt.DesEncryptUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <desc>
 * 运营-开发者授权管理服务层接口实现类
 * </desc>
 *
 * @createDate 2018/12/18
 */
@Service
public class OperateDeveloperAuthServiceImpl implements IOperateDeveloperAuthService {

    private final static Logger LOG = LoggerFactory.getLogger(OperateDeveloperAuthServiceImpl.class);

    @Autowired
    private IOperateDeveloperAuthDao iOperateDeveloperAuthDao;

    @Autowired
    IOperatePropertyAccountDao iOperatePropertyAccountDao;

    @Autowired
    RedisService redisService;

    /**
     * <desc>
     * 获取开发者授权信息分页列表
     * </desc>
     *
     * @param dto 查询参数DTO
     * @return
     * @author Qiang.S
     * @createDate 2018/12/19
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateDeveloperAuthVO> getDeveloperAuthByPage(OperateLoggerDeveloperAuthGetPageDTO dto) throws Exception {
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(dto);
        //分页类默认pageSize为20
        Page<OperateDeveloperAuthVO> page = new Page<>();
        page.getPage().setPageSize(dto.getPageSize());
        page.getPage().setCurrentPage(dto.getCurrentPage());
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        if (dto.getRoleId().length() != 0) {
            params.put("roleId", dto.getRoleId().split(","));
        }
        //填充返回数据集
        List<OperateDeveloperAuthVO> list = iOperateDeveloperAuthDao.getPage(params);
        if (list.size() > 0) {
            //重新查询用户的项目信息
            String[] userIds = new String[list.size()];
            //使用for循环得到数组
            for (int i = 0; i < list.size(); i++) {
                userIds[i] = list.get(i).getUserId();
            }
            //获取全部项目信息
            List<OperateDeveloperAuthProjectInfoVO> listProjectInfo = iOperateDeveloperAuthDao.getProjectInfoByUserId(userIds);
            List<OperateDeveloperAuthProjectInfoVO> listProjectInfoForUserId = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //循环项目信息拼接endTime并给timeStatus赋值
            String userId = null;
            for (int i = 0; i < listProjectInfo.size(); i++) {
                if (listProjectInfo.get(i).getEndTime() == null) {
                    listProjectInfo.get(i).setEndTime("未授权");
                    listProjectInfo.get(i).setTimeStatus("1");
                } else {
                    //有效期和当前时间的时间差
                    Long times = new Long((sdf.parse(listProjectInfo.get(i).getEndTime()).getTime()) - (new Date().getTime()));
                    Long days = times / new Long(1000 * 3600 * 24);
                    if (times < 0) {//有效期过期
                        listProjectInfo.get(i).setEndTime(listProjectInfo.get(i).getEndTime() + " (已经过期)");
                        listProjectInfo.get(i).setTimeStatus("4");
                    } else if (days >= 30 && days < 18250) {//有效期大于1个月
                        listProjectInfo.get(i).setEndTime(listProjectInfo.get(i).getEndTime() + " (正常)");
                        listProjectInfo.get(i).setTimeStatus("2");
                    } else if (days < 30 && days >= 0) {//有效期小于1个月
                        listProjectInfo.get(i).setEndTime(listProjectInfo.get(i).getEndTime() + " (即将过期)");
                        listProjectInfo.get(i).setTimeStatus("3");
                    } else if (days >= 18250) {//有效期大于50年
                        listProjectInfo.get(i).setEndTime("长期有效");
                        listProjectInfo.get(i).setTimeStatus("5");
                    }
                }
                //兼容旧数据 判断授权开始时间是否存在，如果为空，旧数据没有开始时间  默认为 创建开发者的时间。
                if ("".equals(listProjectInfo.get(i).getStartTime()) || null == listProjectInfo.get(i).getStartTime()) {
                    listProjectInfo.get(i).setStartTime(listProjectInfo.get(i).getCreateTime());
                }
                String projectNameFlag = "0";
                String timeStatusFlag = "0";
                String projectIdFlag = "0";
                //当前用户编号不等于上一条用户编号 则进行重新匹配
                if (userId != null && !userId.equals(listProjectInfo.get(i).getUserId())) {
                    for (OperateDeveloperAuthVO operateDeveloperAuthVO : list) {
                        if (operateDeveloperAuthVO.getUserId().equals(userId)) {
                            operateDeveloperAuthVO.setProjectInfo(listProjectInfoForUserId);
                        }
                    }
                    //如果有查询条件则进行判断是否包含
                    if (dto.getProjectName() != null && !"".equals(dto.getProjectName())) {
                        if ((listProjectInfo.get(i).getProjectName().indexOf(dto.getProjectName().toLowerCase()) == -1 &&
                                listProjectInfo.get(i).getProjectName().indexOf(dto.getProjectName().toUpperCase()) == -1 &&
                                listProjectInfo.get(i).getProjectName().indexOf(dto.getProjectName()) == -1)) {
                            projectNameFlag = "1";
                        }
                    }
                    if (dto.getTimeStatus() != null && !"".equals(dto.getTimeStatus())) {
                        //如果有查询条件则进行判断是否包含
                        if (!dto.getTimeStatus().equals(listProjectInfo.get(i).getTimeStatus())) {
                            timeStatusFlag = "1";
                        }
                    }
                    if (dto.getProjectId() != null && !"".equals(dto.getProjectId())) {
                        //如果有查询条件则进行判断是否包含
                        if (!dto.getProjectId().equals(listProjectInfo.get(i).getProjectId())) {
                            projectIdFlag = "1";
                        }
                    }

                    listProjectInfoForUserId = new ArrayList<>();//初始化
                    if ("0".equals(timeStatusFlag) && "0".equals(projectNameFlag) && "0".equals(projectIdFlag)) {
                        listProjectInfoForUserId.add(listProjectInfo.get(i));
                    }
                } else {
                    //如果有查询条件则进行判断是否包含
                    if (dto.getProjectName() != null && !"".equals(dto.getProjectName())) {
                        if ((listProjectInfo.get(i).getProjectName().indexOf(dto.getProjectName().toLowerCase()) == -1 &&
                                listProjectInfo.get(i).getProjectName().indexOf(dto.getProjectName().toUpperCase()) == -1 &&
                                listProjectInfo.get(i).getProjectName().indexOf(dto.getProjectName()) == -1)) {
                            projectNameFlag = "1";
                        }
                    }
                    if (dto.getTimeStatus() != null && !"".equals(dto.getTimeStatus())) {
                        //如果有查询条件则进行判断是否包含
                        if (!dto.getTimeStatus().equals(listProjectInfo.get(i).getTimeStatus())) {
                            timeStatusFlag = "1";
                        }
                    }
                    if (dto.getProjectId() != null && !"".equals(dto.getProjectId())) {
                        //如果有查询条件则进行判断是否包含
                        if (!dto.getProjectId().equals(listProjectInfo.get(i).getProjectId())) {
                            projectIdFlag = "1";
                        }
                    }


                    if ("0".equals(timeStatusFlag) && "0".equals(projectNameFlag) && "0".equals(projectIdFlag)) {
                        listProjectInfoForUserId.add(listProjectInfo.get(i));
                    }
                }
                userId = listProjectInfo.get(i).getUserId();//记录上一次用户编号
                if (i == listProjectInfo.size() - 1) {
                    for (OperateDeveloperAuthVO operateDeveloperAuthVO : list) {
                        if (operateDeveloperAuthVO.getUserId().equals(userId)) {
                            operateDeveloperAuthVO.setProjectInfo(listProjectInfoForUserId);
                        }
                    }
                }
            }
        }
        page.setRows(list, iOperateDeveloperAuthDao.getPageCount(params));
        return page;
    }

    /**
     * <desc>
     * 安全设置
     * </desc>
     *
     * @param operateDeveloperAuthSafeSettingsDTO 安全设置DTO
     * @return 受影响的行数
     * @author wangzhi
     * @createDate 2018/12/18
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Integer safeSettings(OperateDeveloperAuthSafeSettingsDTO operateDeveloperAuthSafeSettingsDTO) throws Exception {
        int intRes = 0;
        Date nowTime = new Date();
        if (operateDeveloperAuthSafeSettingsDTO.getType() == 0) { // 绑定IP
            intRes += iOperateDeveloperAuthDao.deleteByType(operateDeveloperAuthSafeSettingsDTO.getUserId(), 1);
            if (StringUtils.isNotBlank(operateDeveloperAuthSafeSettingsDTO.getData())) {
                List<String> ips = StringHandlerUtil.splitStringList(operateDeveloperAuthSafeSettingsDTO.getData());
                List<DeveloperAccountAuthPO> list = new ArrayList<>();
                for (String ip : ips) {
                    DeveloperAccountAuthPO developerAccountAuthPO = new DeveloperAccountAuthPO();
                    developerAccountAuthPO.setAuthId(LogicIdUtil.bussinessId());
                    developerAccountAuthPO.setUserId(operateDeveloperAuthSafeSettingsDTO.getUserId());
                    developerAccountAuthPO.setDataType(1);
                    developerAccountAuthPO.setBindingIp(ip);
                    developerAccountAuthPO.setStatus(DataStatusEnum.NORMAL.getType());
                    developerAccountAuthPO.setCreateTime(nowTime);
                    developerAccountAuthPO.setUpdateTime(nowTime);
                    list.add(developerAccountAuthPO);
                }
                intRes += list.size();
                iOperateDeveloperAuthDao.batchSaveIp(list);
            }
        } else { // 绑定MAC
            intRes += iOperateDeveloperAuthDao.deleteByType(operateDeveloperAuthSafeSettingsDTO.getUserId(), 2);
            if (StringUtils.isNotBlank(operateDeveloperAuthSafeSettingsDTO.getData())) {
                List<String> macs = StringHandlerUtil.splitStringList(operateDeveloperAuthSafeSettingsDTO.getData());
                List<DeveloperAccountAuthPO> list = new ArrayList<>();
                for (String mac : macs) {
                    DeveloperAccountAuthPO developerAccountAuthPO = new DeveloperAccountAuthPO();
                    developerAccountAuthPO.setAuthId(LogicIdUtil.bussinessId());
                    developerAccountAuthPO.setUserId(operateDeveloperAuthSafeSettingsDTO.getUserId());
                    developerAccountAuthPO.setDataType(2);
                    developerAccountAuthPO.setBindingMac(mac);
                    developerAccountAuthPO.setStatus(DataStatusEnum.NORMAL.getType());
                    developerAccountAuthPO.setCreateTime(nowTime);
                    developerAccountAuthPO.setUpdateTime(nowTime);
                    list.add(developerAccountAuthPO);
                }
                intRes += list.size();
                iOperateDeveloperAuthDao.batchSaveMac(list);
            }
        }
        return intRes;
    }

    /**
     * <desc>
     * 有效期延期设置
     * </desc>
     *
     * @param operateDeveloperAuthMoreValidityDTO 有效期延期设置DTO
     * @return 受影响行数
     * @author wangzhi
     * @createDate 2018/12/18
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Integer moreValidity(OperateDeveloperAuthMoreValidityDTO operateDeveloperAuthMoreValidityDTO) throws Exception {
        Map<String, Object> map = new HashMap<>();
        //判断授权有效期设置类型(0表示自定义,1表示长期有效)
        if ("0".equals(operateDeveloperAuthMoreValidityDTO.getAuthValidityPeriodSetType() + "")) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date startTime = sdf.parse(operateDeveloperAuthMoreValidityDTO.getStartTime());
            Date endTime = sdf.parse(operateDeveloperAuthMoreValidityDTO.getEndTime());
            Calendar cal1 = Calendar.getInstance();
            Calendar startTimecal = Calendar.getInstance();
            //开始时间
            startTimecal.setTime(startTime);
            startTimecal.set(Calendar.HOUR_OF_DAY, 00);
            startTimecal.set(Calendar.MINUTE, 00);
            startTimecal.set(Calendar.SECOND, 00);
            //结束时间
            cal1.setTime(endTime);
            cal1.set(Calendar.HOUR_OF_DAY, 23);
            cal1.set(Calendar.MINUTE, 59);
            cal1.set(Calendar.SECOND, 59);
            map.put("startTime", startTimecal.getTime());
            map.put("endTime", cal1.getTime());
            OperatePropertyAccountDetailVO detail = iOperatePropertyAccountDao.getDetail(operateDeveloperAuthMoreValidityDTO.getUserId());
            // 一卡通开发者时更新牌照
            if ("6".equals(detail.getRoleId())) {
                Map<String, Object> licenceMap = iOperatePropertyAccountDao.getLicenceInfo(operateDeveloperAuthMoreValidityDTO.getUserId(),operateDeveloperAuthMoreValidityDTO.getProjectId());
                String appsecret = licenceMap.get("appsecret").toString();
                licenceMap.remove("appsecret");
                licenceMap.put("startTime", new SimpleDateFormat(DateUtil.DATE_FORMAT_TYPE1).format(startTime));
                licenceMap.put("endTime", new SimpleDateFormat(DateUtil.DATE_FORMAT_TYPE1).format(cal1.getTime()));
                String licence = DesEncryptUtil.aesEncryptSDK(JSON.toJSONString(licenceMap), appsecret);
                PropertyUserInfoPO userInfoPO = new PropertyUserInfoPO();
                userInfoPO.setUserId(operateDeveloperAuthMoreValidityDTO.getUserId());
                //userInfoPO.setLicence(licence);
                userInfoPO.setUpdateTime(new Date());
                iOperatePropertyAccountDao.updateProjectInfoLicenceByProjectId(operateDeveloperAuthMoreValidityDTO.getProjectId(), licence, userInfoPO.getUserId(), detail.getRoleId());
                iOperatePropertyAccountDao.update(SqlUtil.durableData(userInfoPO, PlatformConstants.TABLE_UPDATE));
            }
        } else {//长期有效
            OperatePropertyAccountDetailVO detail = iOperatePropertyAccountDao.getDetail(operateDeveloperAuthMoreValidityDTO.getUserId());
            // 一卡通开发者时更新牌照
            Date date = new Date();
            Date startdate = new Date();
            //设置开发者牌照过期时间，为当前时间+100年，这个时候你我人都没有
            date.setYear(220);
            if ("6".equals(detail.getRoleId())) {
                Map<String, Object> licenceMap = iOperatePropertyAccountDao.getLicenceInfo(operateDeveloperAuthMoreValidityDTO.getUserId(),operateDeveloperAuthMoreValidityDTO.getProjectId());
                String appsecret = licenceMap.get("appsecret").toString();
                licenceMap.remove("appsecret");
                licenceMap.put("startTime", new SimpleDateFormat(DateUtil.DATE_FORMAT_TYPE1).format(startdate));
                licenceMap.put("endTime", new SimpleDateFormat(DateUtil.DATE_FORMAT_TYPE1).format(date));
                String licence = DesEncryptUtil.aesEncryptSDK(JSON.toJSONString(licenceMap), appsecret);
                PropertyUserInfoPO userInfoPO = new PropertyUserInfoPO();
                userInfoPO.setUserId(operateDeveloperAuthMoreValidityDTO.getUserId());
                //userInfoPO.setLicence(licence);
                userInfoPO.setUpdateTime(new Date());
                iOperatePropertyAccountDao.updateProjectInfoLicenceByProjectId(operateDeveloperAuthMoreValidityDTO.getProjectId(), licence, userInfoPO.getUserId(), detail.getRoleId());
                iOperatePropertyAccountDao.update(SqlUtil.durableData(userInfoPO, PlatformConstants.TABLE_UPDATE));
            }
            map.put("startTime", new SimpleDateFormat(DateUtil.DATE_FORMAT_TYPE1).format(startdate));
            map.put("endTime", new SimpleDateFormat(DateUtil.DATE_FORMAT_TYPE1).format(date));
        }
        map.put("userId", operateDeveloperAuthMoreValidityDTO.getUserId());
        map.put("projectId", operateDeveloperAuthMoreValidityDTO.getProjectId());
        map.put("authValidityPeriodSetType", operateDeveloperAuthMoreValidityDTO.getAuthValidityPeriodSetType());
        map.put("description", operateDeveloperAuthMoreValidityDTO.getDescription());
        return iOperateDeveloperAuthDao.moreValidity(map);
    }

    /**
     * <desc>
     * 获取设备授权列表
     * </desc>
     *
     * @param operateDeveloperAuthDeviceAuthListDTO 设备授权列表DTO
     * @return 设备授权列表VO
     * @author wangzhi
     * @createDate 2018/12/18
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public OperateDeveloperAuthDeviceAuthVO deviceAuthList(OperateDeveloperAuthDeviceAuthListDTO operateDeveloperAuthDeviceAuthListDTO) throws Exception {
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(operateDeveloperAuthDeviceAuthListDTO);
        PropertyRoleProjectPO propertyRoleProjectPO = iOperateDeveloperAuthDao.getOriginalProjectInfoByRole4(params);
        OperateDeveloperAuthDeviceAuthVO operateDeveloperAuthDeviceAuthVO = new OperateDeveloperAuthDeviceAuthVO();
        List<OperateDeveloperAuthDeviceAuthListVO> deviceAuth = iOperateDeveloperAuthDao.getDeviceList(params);
        List<OperateDeveloperAuthDeviceAuthListVO> deviceAuthCloudElevator = iOperateDeveloperAuthDao.getDeviceListCloudElevator(params);
        List<OperateDeveloperAuthDeviceAuthListVO> deviceAuthCloudEntrance = iOperateDeveloperAuthDao.getDeviceListCloudEntrance(params);
        if (propertyRoleProjectPO != null) {
            //去除项目已被其他开发者占用的影响点
           /* if (propertyRoleProjectPO.getCloudIntercomSdk() == null || propertyRoleProjectPO.getCloudIntercomSdk() == 0) {
                int count = iOperateDeveloperAuthDao.getCloudIntercomSdkCount(operateDeveloperAuthDeviceAuthListDTO.getProjectId());
                if (count > 0) {
                    propertyRoleProjectPO.setCloudIntercomSdk(2);
                }
            }
            if (propertyRoleProjectPO.getCloudElevatorApi() == null || propertyRoleProjectPO.getCloudElevatorApi() == 0) {
                int count = iOperateDeveloperAuthDao.getCloudElevatorApiCount(operateDeveloperAuthDeviceAuthListDTO.getProjectId());
                if (count > 0) {
                    propertyRoleProjectPO.setCloudElevatorApi(2);
                }
            }*/
            operateDeveloperAuthDeviceAuthVO.setCloudIntercomSdk(propertyRoleProjectPO.getCloudIntercomSdk());
            operateDeveloperAuthDeviceAuthVO.setBluetoothSdk(propertyRoleProjectPO.getBluetoothSdk());
            //operateDeveloperAuthDeviceAuthVO.setQrCodeSdk(propertyRoleProjectPO.getQrCodeSdk());
            operateDeveloperAuthDeviceAuthVO.setCloudElevatorApi(propertyRoleProjectPO.getCloudElevatorApi());
            operateDeveloperAuthDeviceAuthVO.setRobotElevatorApi(propertyRoleProjectPO.getRobotElevatorApi());
            operateDeveloperAuthDeviceAuthVO.setCloudEntranceApi(propertyRoleProjectPO.getCloudEntranceApi());
            operateDeveloperAuthDeviceAuthVO.setVisitorMachineApi(propertyRoleProjectPO.getVisitorMachineApi());
            operateDeveloperAuthDeviceAuthVO.setFaceRecognitionApi(propertyRoleProjectPO.getFaceRecognitionApi());
            operateDeveloperAuthDeviceAuthVO.setCloudElevatorStatus(propertyRoleProjectPO.getCloudElevatorStatus());
            operateDeveloperAuthDeviceAuthVO.setCloudEntranceStatus(propertyRoleProjectPO.getCloudEntranceStatus());
            operateDeveloperAuthDeviceAuthVO.setOfflineQRCodeSdk(propertyRoleProjectPO.getOfflineQRCodeSdk());
            operateDeveloperAuthDeviceAuthVO.setWechatSmallBluetoothDocking(propertyRoleProjectPO.getWechatSmallBluetoothDocking());
            operateDeveloperAuthDeviceAuthVO.setWechatSmallQRCode(propertyRoleProjectPO.getWechatSmallQRCode());
            operateDeveloperAuthDeviceAuthVO.setOnlineQRCodeApi(propertyRoleProjectPO.getOnlineQRCodeApi());
            if (propertyRoleProjectPO.getFunctionAuth() != null) {
                operateDeveloperAuthDeviceAuthVO.setFunctionAuth(StringHandlerUtil.splitStringList(propertyRoleProjectPO.getFunctionAuth()));
            }
            // 从机器人乘梯API授权设备信息中解析出deviceId
            String deviceAuthStr = propertyRoleProjectPO.getDeviceAuth();
            if (StringUtils.isNotBlank(deviceAuthStr)) {
                int i = deviceAuthStr.indexOf("1");
                while (i != -1) {
                    int deviceInt = i + 1;
                    String deviceId = StringHandlerUtil.formatStrLen(deviceInt + "", "0", 4, false, false);
                    for (OperateDeveloperAuthDeviceAuthListVO operateDeveloperAuthDeviceAuthListVO : deviceAuth) {
                        if (deviceId.equals(operateDeveloperAuthDeviceAuthListVO.getDeviceId())) {
                            operateDeveloperAuthDeviceAuthListVO.setAuthStatus(1);
                            break;
                        }
                    }
                    i = deviceAuthStr.indexOf("1", deviceInt);
                }
            }
            // 从云电梯API授权设备信息解析出deviceId
            String deviceAuthCloudElevatorStr = propertyRoleProjectPO.getDeviceAuthCloudElevator();
            if (StringUtils.isNotBlank(deviceAuthCloudElevatorStr)) {
                int i = deviceAuthCloudElevatorStr.indexOf("1");
                while (i != -1) {
                    int deviceInt = i + 1;
                    String deviceId = StringHandlerUtil.formatStrLen(deviceInt + "", "0", 4, false, false);
                    for (OperateDeveloperAuthDeviceAuthListVO operateDeveloperAuthDeviceAuthListVO : deviceAuthCloudElevator) {
                        if (deviceId.equals(operateDeveloperAuthDeviceAuthListVO.getDeviceId())) {
                            operateDeveloperAuthDeviceAuthListVO.setAuthStatus(1);
                            break;
                        }
                    }
                    i = deviceAuthCloudElevatorStr.indexOf("1", deviceInt);
                }
            }
            // 从云门禁API授权设备信息解析出deviceId
            String deviceAuthCloudEntranceStr = propertyRoleProjectPO.getDeviceAuthCloudEntrance();
            if (StringUtils.isNotBlank(deviceAuthCloudEntranceStr)) {
                int i = deviceAuthCloudEntranceStr.indexOf("1");
                while (i != -1) {
                    int deviceInt = i + 1;
                    String deviceId = StringHandlerUtil.formatStrLen(deviceInt + "", "0", 4, false, false);
                    for (OperateDeveloperAuthDeviceAuthListVO operateDeveloperAuthDeviceAuthListVO : deviceAuthCloudEntrance) {
                        if (deviceId.equals(operateDeveloperAuthDeviceAuthListVO.getDeviceId())) {
                            operateDeveloperAuthDeviceAuthListVO.setAuthStatus(1);
                            break;
                        }
                    }
                    i = deviceAuthCloudEntranceStr.indexOf("1", deviceInt);
                }
            }
        }
        operateDeveloperAuthDeviceAuthVO.setDeviceAuth(deviceAuth);
        operateDeveloperAuthDeviceAuthVO.setDeviceAuthCloudElevator(deviceAuthCloudElevator);
        operateDeveloperAuthDeviceAuthVO.setDeviceAuthCloudEntrance(deviceAuthCloudEntrance);
        String robotId = iOperateDeveloperAuthDao.getRobotId(operateDeveloperAuthDeviceAuthListDTO.getUserId(), operateDeveloperAuthDeviceAuthListDTO.getProjectId());
        if (robotId != null && !"".equals(robotId) && ",".equals(robotId.substring(robotId.length() - 1, robotId.length()))) {
            robotId = robotId.substring(0, robotId.length() - 1);
        }
        operateDeveloperAuthDeviceAuthVO.setRobotId(robotId);
        return operateDeveloperAuthDeviceAuthVO;
    }

    /**
     * <desc>
     * 机器人开发者获取设备授权列表
     * </desc>
     *
     * @param operateDeveloperAuthDeviceAuthListDTO 设备授权列表DTO
     * @return 设备授权列表VO
     * @author Qiang.S
     * @createDate 2020/03/20
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public OperateRobotDeveloperDeviceAuthVO robotDeviceAuthList(OperateDeveloperAuthDeviceAuthListDTO operateDeveloperAuthDeviceAuthListDTO) throws Exception {
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(operateDeveloperAuthDeviceAuthListDTO);
        OperateRobotDeveloperDeviceAuthVO operateRobotDeveloperDeviceAuthVO = iOperateDeveloperAuthDao.getRobotDevelopAuth(params);
        List<OperateDeveloperAuthDeviceAuthListVO> deviceAuthCloudElevator = iOperateDeveloperAuthDao.getDeviceListCloudElevator(params);
        List<OperateDeveloperAuthDeviceAuthListVO> deviceAuthCloudEntrance = iOperateDeveloperAuthDao.getDeviceListCloudEntrance(params);
        List<String> cloudControllerElevatorIdList = iOperateDeveloperAuthDao.getCloudControllerElevator(params);
        if (operateRobotDeveloperDeviceAuthVO != null) {
            operateRobotDeveloperDeviceAuthVO.setRobotIds(iOperateDeveloperAuthDao.getRobotDevelopRobotInfo(params));
        }
        //查询机器人开发者设备机器人相关信息
        List<Map<String, Object>> robotDevelopInfo = iOperateDeveloperAuthDao.getRobotDevelopInfo(params);
        //多梯列表
        List<OperateDeveloperAuthDeviceAuthListVO> noninductiveList = new ArrayList<>();
        //单梯列表
        List<OperateRobotDeveloperDeviceAuthListVO> robotNoninductiveList = new ArrayList<>();
        List<OperateRobotDeveloperDeviceAuthListVO> robotToCloudList = new ArrayList<>();
        List<OperateRobotDeveloperDeviceAuthListVO> robotEntranceList = new ArrayList<>();
        //分别找出单梯和多梯列表
        for (OperateDeveloperAuthDeviceAuthListVO deviceAuthListVO : deviceAuthCloudElevator) {
            if (cloudControllerElevatorIdList.contains(deviceAuthListVO.getDeviceId())) {
                noninductiveList.add(deviceAuthListVO);
            }
        }
        for (OperateDeveloperAuthDeviceAuthListVO robotNoninductiveVO : noninductiveList) {
            OperateRobotDeveloperDeviceAuthListVO vo = new OperateRobotDeveloperDeviceAuthListVO();
            vo.setDeviceUnique(operateDeveloperAuthDeviceAuthListDTO.getProjectId() + "1" + robotNoninductiveVO.getDeviceId());
            vo.setDeviceName(robotNoninductiveVO.getDeviceName());
            vo.setIsOnline(redisService.get(RedisConstant.DEVICE_ELEVATOR_IS_ONLINE_KEY +
                    operateDeveloperAuthDeviceAuthListDTO.getProjectId() + "1" + robotNoninductiveVO.getDeviceId()) == null ? "0" : "1");
            if (robotDevelopInfo != null && robotDevelopInfo.size() > 0) {
                for (Map<String, Object> robotDevelopInfoMap : robotDevelopInfo) {
                    if (robotDevelopInfoMap.get("deviceUnique").toString().equals(vo.getDeviceUnique())
                            && "1".equals(robotDevelopInfoMap.get("deviceType").toString())) {//1多梯自动调度云对接
                        vo.setDeviceCheck(true);
                        vo.setRobotId(robotDevelopInfoMap.get("roborId") == null ? "" : robotDevelopInfoMap.get("roborId").toString());
                        break;
                    }
                }
            }
            robotNoninductiveList.add(vo);
        }
        for (OperateDeveloperAuthDeviceAuthListVO robotToCloudVO : deviceAuthCloudElevator) {
            OperateRobotDeveloperDeviceAuthListVO vo = new OperateRobotDeveloperDeviceAuthListVO();
            vo.setDeviceUnique(operateDeveloperAuthDeviceAuthListDTO.getProjectId() + "1" + robotToCloudVO.getDeviceId());
            vo.setDeviceName(robotToCloudVO.getDeviceName());
            vo.setIsOnline(redisService.get(RedisConstant.DEVICE_ELEVATOR_IS_ONLINE_KEY +
                    operateDeveloperAuthDeviceAuthListDTO.getProjectId() + "1" + robotToCloudVO.getDeviceId()) == null ? "0" : "1");
            if (robotDevelopInfo != null && robotDevelopInfo.size() > 0) {
                for (Map<String, Object> robotDevelopInfoMap : robotDevelopInfo) {
                    if (robotDevelopInfoMap.get("deviceUnique").toString().equals(vo.getDeviceUnique())
                            && "2".equals(robotDevelopInfoMap.get("deviceType").toString())) {//2单梯云对接
                        vo.setDeviceCheck(true);
                        vo.setRobotId(robotDevelopInfoMap.get("roborId") == null ? "" : robotDevelopInfoMap.get("roborId").toString());
                        break;
                    }
                }
            }
            robotToCloudList.add(vo);
        }
        for (OperateDeveloperAuthDeviceAuthListVO robotEntranceVO : deviceAuthCloudEntrance) {
            OperateRobotDeveloperDeviceAuthListVO vo = new OperateRobotDeveloperDeviceAuthListVO();
            vo.setDeviceUnique(operateDeveloperAuthDeviceAuthListDTO.getProjectId() + "2" + robotEntranceVO.getDeviceId());
            vo.setDeviceName(robotEntranceVO.getDeviceName());
            vo.setIsOnline((redisService.get(RedisConstant.DEVICE_ENTRANCE_IS_ONLINE_KEY +
                    operateDeveloperAuthDeviceAuthListDTO.getProjectId() + "2" + robotEntranceVO.getDeviceId()) == null
                    && redisService.get(RedisConstant.DEVICE_WIPPEN_IS_ONLINE_KEY +
                    operateDeveloperAuthDeviceAuthListDTO.getProjectId() + "2" + robotEntranceVO.getDeviceId()) == null) ? "0" : "1");
            if (robotDevelopInfo != null && robotDevelopInfo.size() > 0) {
                for (Map<String, Object> robotDevelopInfoMap : robotDevelopInfo) {
                    if (robotDevelopInfoMap.get("deviceUnique").toString().equals(vo.getDeviceUnique())
                            && "3".equals(robotDevelopInfoMap.get("deviceType").toString())) {//3云门禁云对接
                        vo.setDeviceCheck(true);
                        vo.setRobotId(robotDevelopInfoMap.get("roborId") == null ? "" : robotDevelopInfoMap.get("roborId").toString());
                        break;
                    }
                }
            }
            robotEntranceList.add(vo);
        }
        if (operateRobotDeveloperDeviceAuthVO != null) {
            operateRobotDeveloperDeviceAuthVO.setRobotNoninductiveInfo(robotNoninductiveList.size() == 0 ? "" : JSONArray.parseArray(JSON.toJSONString(robotNoninductiveList)).toJSONString());
            operateRobotDeveloperDeviceAuthVO.setRobotToCloudInfo(robotToCloudList.size() == 0 ? "" : JSONArray.parseArray(JSON.toJSONString(robotToCloudList)).toJSONString());
            operateRobotDeveloperDeviceAuthVO.setRobotEntranceInfo(robotEntranceList.size() == 0 ? "" : JSONArray.parseArray(JSON.toJSONString(robotEntranceList)).toJSONString());
        }
        return operateRobotDeveloperDeviceAuthVO;
    }

    /**
     * <desc>
     * 设备授权
     * </desc>
     *
     * @param operateDeveloperAuthDeviceAuthDTO 设备授权DTO
     * @return 受影响行数
     * @author wangzhi
     * @createDate 2018/12/18
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Integer deviceAuth(OperateDeveloperAuthDeviceAuthDTO operateDeveloperAuthDeviceAuthDTO) throws Exception {
        if (!"6".equals(operateDeveloperAuthDeviceAuthDTO.getRoleId())) {
            // 初始化电梯设备授权字符串
            String deviceAuthStr = StringHandlerUtil.formatStrLen("0", "0", 384, false, false);
            StringBuilder deviceAuth = new StringBuilder(deviceAuthStr);
            // 初始化电梯设备授权字符串
            String deviceAuthCloudElevatorStr = StringHandlerUtil.formatStrLen("0", "0", 384, false, false);
            StringBuilder deviceAuthCloudElevator = new StringBuilder(deviceAuthCloudElevatorStr);


            // 处理机器人电梯设备授权字符串
            if (StringUtils.isNotBlank(operateDeveloperAuthDeviceAuthDTO.getDeviceId())) {
                List<String> deviceIdList = StringHandlerUtil.splitStringList(operateDeveloperAuthDeviceAuthDTO.getDeviceId());
                for (String deviceId : deviceIdList) {
                    String str = deviceId.replaceAll("^(0+)", "");
                    int index = StringHandlerUtil.strToIntDef(str, -1) - 1;
                    if (index < 0 || index > 383) {
                        continue;
                    }
                    deviceAuth = deviceAuth.replace(index, index + 1, "1");
                }
            }
            // 情况1 当云电梯API为1是 ,默认为0  当云电梯对接状态为1是 ,默认为0
            if (1 == operateDeveloperAuthDeviceAuthDTO.getCloudElevatorApi() && 1 == operateDeveloperAuthDeviceAuthDTO.getCloudElevatorStatus()) {
                // 处理电梯API电梯设备授权字符串
                if (StringUtils.isNotBlank(operateDeveloperAuthDeviceAuthDTO.getDeviceIdCloudElevator())) {
                    List<String> deviceIdList = StringHandlerUtil.splitStringList(operateDeveloperAuthDeviceAuthDTO.getDeviceIdCloudElevator());
                    for (String deviceId : deviceIdList) {
                        String str = deviceId.replaceAll("^(0+)", "");
                        int index = StringHandlerUtil.strToIntDef(str, -1) - 1;
                        if (index < 0 || index > 383) {
                            continue;
                        }
                        deviceAuthCloudElevator = deviceAuthCloudElevator.replace(index, index + 1, "1");
                    }
                }
            }
            //情况2 当云电梯API 为1是, 默认为0  当云电梯对接状态 为0否 , 默认为0  时查询项目下所有绑定的电梯
            if (1 == operateDeveloperAuthDeviceAuthDTO.getCloudElevatorApi() && 0 == operateDeveloperAuthDeviceAuthDTO.getCloudElevatorStatus()) {
                //获取项目下所有绑定的电梯
                List<OperateDeveloperAuthDeviceAuthListVO> listCloudElevator = iOperateDeveloperAuthDao.getDeviceListCloudElevator(SqlUtil.convertBeanToMapParams(operateDeveloperAuthDeviceAuthDTO));
                for (OperateDeveloperAuthDeviceAuthListVO authListVO : listCloudElevator) {
                    String str = authListVO.getDeviceId().replaceAll("^(0+)", "");
                    int index = StringHandlerUtil.strToIntDef(str, -1) - 1;
                    if (index < 0 || index > 383) {
                        continue;
                    }
                    deviceAuthCloudElevator = deviceAuthCloudElevator.replace(index, index + 1, "1");
                }

            }

            // 初始化门禁设备授权字符串
            String deviceAuthCloudEntranceStr = StringHandlerUtil.formatStrLen("0", "0", 2048, false, false);
            StringBuilder deviceAuthCloudEntrance = new StringBuilder(deviceAuthCloudEntranceStr);
            // 情况1 当云门禁API 为1是  0否，1是 默认为0  当云门禁对接状态 0否，1是 默认为0
            if (1 == operateDeveloperAuthDeviceAuthDTO.getCloudEntranceApi() &&
                    1 == operateDeveloperAuthDeviceAuthDTO.getCloudEntranceStatus()) {
                // 处理门禁设备授权字符串
                if (StringUtils.isNotBlank(operateDeveloperAuthDeviceAuthDTO.getDeviceIdCloudEntrance())) {
                    List<String> deviceIdList = StringHandlerUtil.splitStringList(operateDeveloperAuthDeviceAuthDTO.getDeviceIdCloudEntrance());
                    for (String deviceId : deviceIdList) {
                        String str = deviceId.replaceAll("^(0+)", "");
                        int index = StringHandlerUtil.strToIntDef(str, -1) - 1;
                        if (index < 0 || index > 2047) {
                            continue;
                        }
                        deviceAuthCloudEntrance = deviceAuthCloudEntrance.replace(index, index + 1, "1");
                    }
                }
            }
            //情况2 当云门禁API 为1是  0否，1是 默认为0  当云门禁对接状态为0否 0否，1是 默认为0
            if (1 == operateDeveloperAuthDeviceAuthDTO.getCloudEntranceApi() &&
                    0 == operateDeveloperAuthDeviceAuthDTO.getCloudEntranceStatus()) {
                //获取项目下所有绑定的门禁
                List<OperateDeveloperAuthDeviceAuthListVO> listCloudEntrance = iOperateDeveloperAuthDao.getDeviceListCloudEntrance(SqlUtil.convertBeanToMapParams(operateDeveloperAuthDeviceAuthDTO));
                for (OperateDeveloperAuthDeviceAuthListVO deviceAuthListVO : listCloudEntrance) {
                    String str = deviceAuthListVO.getDeviceId().replaceAll("^(0+)", "");
                    int index = StringHandlerUtil.strToIntDef(str, -1) - 1;
                    if (index < 0 || index > 2047) {
                        continue;
                    }
                    deviceAuthCloudEntrance = deviceAuthCloudEntrance.replace(index, index + 1, "1");
                }
            }

            Map<String, Object> params = SqlUtil.convertBeanToMapParams(operateDeveloperAuthDeviceAuthDTO);
            params.put("deviceAuth", deviceAuth.toString());
            params.put("deviceAuthCloudElevator", deviceAuthCloudElevator.toString());
            params.put("deviceAuthCloudEntrance", deviceAuthCloudEntrance.toString());
            PropertyRoleProjectPO propertyRoleProjectPO = iOperateDeveloperAuthDao.getOriginalProjectInfoByRole4(params);
            if (operateDeveloperAuthDeviceAuthDTO.getCloudIntercomSdk() == 1 && propertyRoleProjectPO != null && StringUtils.isBlank(propertyRoleProjectPO.getCloudIntercomAppidId())) {
                String cloudIntercomAppidId = iOperateDeveloperAuthDao.getOneCloudIntercomAppidId();
                if (StringUtils.isNotBlank(cloudIntercomAppidId)) {
                    params.put("cloudIntercomAppidId", cloudIntercomAppidId);
                    iOperateDeveloperAuthDao.bindingCloudIntercomAppidId(cloudIntercomAppidId);
                }
            }
            iOperateDeveloperAuthDao.deleteRobot(operateDeveloperAuthDeviceAuthDTO.getUserId(), operateDeveloperAuthDeviceAuthDTO.getProjectId());
            //保存机器人信息
            String[] robotIds = operateDeveloperAuthDeviceAuthDTO.getRobotId().split(",");
            if (robotIds.length > 0) {
                List<DeveloperAccountAuthPO> list = new ArrayList<>();
                for (String robotId : robotIds) {
                    DeveloperAccountAuthPO developerAccountAuthPO = new DeveloperAccountAuthPO();
                    developerAccountAuthPO.setAuthId(LogicIdUtil.bussinessId());
                    developerAccountAuthPO.setUserId(operateDeveloperAuthDeviceAuthDTO.getUserId());
                    developerAccountAuthPO.setProjectId(operateDeveloperAuthDeviceAuthDTO.getProjectId());
                    developerAccountAuthPO.setDataType(3);
                    developerAccountAuthPO.setRobotId(robotId);
                    developerAccountAuthPO.setStatus(DataStatusEnum.NORMAL.getType());
                    developerAccountAuthPO.setCreateTime(new Date());
                    list.add(developerAccountAuthPO);
                }
                iOperateDeveloperAuthDao.batchSaveRobot(list);
            }


            return iOperateDeveloperAuthDao.deviceAuth(params);
        } else {//一卡通开发者
            Map<String, Object> params = new HashMap<>();
            params.put("userId", operateDeveloperAuthDeviceAuthDTO.getUserId());
            params.put("projectId", operateDeveloperAuthDeviceAuthDTO.getProjectId());
            params.put("functionAuth", operateDeveloperAuthDeviceAuthDTO.getFunctionAuth() == null ? "" : operateDeveloperAuthDeviceAuthDTO.getFunctionAuth());

            Map<String, Object> licenceMap = iOperatePropertyAccountDao.getLicenceInfo(operateDeveloperAuthDeviceAuthDTO.getUserId(),operateDeveloperAuthDeviceAuthDTO.getProjectId());
            licenceMap.put("function", operateDeveloperAuthDeviceAuthDTO.getFunctionAuth() == null ? "" : operateDeveloperAuthDeviceAuthDTO.getFunctionAuth());
            String appsecret = licenceMap.get("appsecret").toString();
            licenceMap.remove("appsecret");
            String licence = DesEncryptUtil.aesEncryptSDK(JSON.toJSONString(licenceMap), appsecret);
            PropertyUserInfoPO userInfoPO = new PropertyUserInfoPO();
            userInfoPO.setUserId(operateDeveloperAuthDeviceAuthDTO.getUserId());
            //userInfoPO.setLicence(licence);
            userInfoPO.setUpdateTime(new Date());
            iOperatePropertyAccountDao.updateProjectInfoLicenceByProjectId(operateDeveloperAuthDeviceAuthDTO.getProjectId(), licence, userInfoPO.getUserId(), operateDeveloperAuthDeviceAuthDTO.getRoleId());
            iOperatePropertyAccountDao.update(SqlUtil.durableData(userInfoPO, PlatformConstants.TABLE_UPDATE));
            return iOperateDeveloperAuthDao.deviceAuth(params);
        }
    }

    /**
     * <desc>
     * 接口授权
     * </desc>
     *
     * @param operateDeveloperRobotAuthDeviceAuthDTO 设备授权DTO
     * @return 受影响行数
     * @author Qiang.S
     * @createDate 2020/03/20
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Integer robotDeviceAuth(OperateDeveloperRobotAuthDeviceAuthDTO operateDeveloperRobotAuthDeviceAuthDTO) throws Exception {
        //清除所有数据
        iOperateDeveloperAuthDao.deleteRobotDevelopInfo(operateDeveloperRobotAuthDeviceAuthDTO.getUserId(), operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
        List<RobotDeveloperAccountAuthPO> poList = new ArrayList<>();
        //机器人白名单授权为 1是 0否 并且机器人设备授权为 1是 0否


        //情况1 机器人白名单授权为 1是 并且机器人设备授权为 1是
        if (operateDeveloperRobotAuthDeviceAuthDTO.getRobotWhiteListAuthStatus() != null && operateDeveloperRobotAuthDeviceAuthDTO.getRobotWhiteListAuthStatus() == 1
                && operateDeveloperRobotAuthDeviceAuthDTO.getRobotDeviceAuthStatus() != null && operateDeveloperRobotAuthDeviceAuthDTO.getRobotDeviceAuthStatus() == 1) {
            //处理门禁信息 云门禁云对接
            if (operateDeveloperRobotAuthDeviceAuthDTO.getRobotEntranceInfo() != null && !"".equals(operateDeveloperRobotAuthDeviceAuthDTO.getRobotEntranceInfo()) ) {
                List<Map<String, String>> listObjectFir = (List<Map<String, String>>) JSONArray.parse(operateDeveloperRobotAuthDeviceAuthDTO.getRobotEntranceInfo());
                for (Map<String, String> map : listObjectFir) {
                    String[] robotIds = map.get("robotId").split(",");//机器人白名单授权为 是
                    for (String robotId : robotIds) {
                        RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                        po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                        po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                        po.setDeviceType(3);
                        if ( 1==operateDeveloperRobotAuthDeviceAuthDTO.getRobotEntrance()){
                            po.setDeviceUnique(map.get("deviceUnique"));
                        }else {
                            po.setDeviceUnique("");
                        }

                        po.setRobotId(robotId);
                        po.setStatus(DataStatusEnum.NORMAL.getType());
                        po.setCreateTime(new Date());
                        poList.add(po);
                    }
                }
            }
            //处理机器人无感信息 多梯自动调度
            if (operateDeveloperRobotAuthDeviceAuthDTO.getRobotNoninductiveInfo() != null && !"".equals(operateDeveloperRobotAuthDeviceAuthDTO.getRobotNoninductiveInfo())) {
                List<Map<String, String>> listObjectFir = (List<Map<String, String>>) JSONArray.parse(operateDeveloperRobotAuthDeviceAuthDTO.getRobotNoninductiveInfo());
                for (Map<String, String> map : listObjectFir) {
                    String[] robotIds = map.get("robotId").split(",");//机器人白名单授权为 是
                    for (String robotId : robotIds) {
                        RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                        po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                        po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                        po.setDeviceType(1);
                        if (1==operateDeveloperRobotAuthDeviceAuthDTO.getRobotNoninductive()){
                            po.setDeviceUnique(map.get("deviceUnique"));
                        }else {
                            po.setDeviceUnique("");
                        }
                        po.setRobotId(robotId);
                        po.setStatus(DataStatusEnum.NORMAL.getType());
                        po.setCreateTime(new Date());
                        poList.add(po);
                    }
                }
            }
            ////机器人对云端信息 单梯云对接
            if (operateDeveloperRobotAuthDeviceAuthDTO.getRobotToCloudInfo() != null && !"".equals(operateDeveloperRobotAuthDeviceAuthDTO.getRobotToCloudInfo())) {
                List<Map<String, String>> listObjectFir = (List<Map<String, String>>) JSONArray.parse(operateDeveloperRobotAuthDeviceAuthDTO.getRobotToCloudInfo());
                for (Map<String, String> map : listObjectFir) {
                    String[] robotIds = map.get("robotId").split(",");//机器人白名单授权为 是
                    for (String robotId : robotIds) {
                        RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                        po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                        po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                        po.setDeviceType(2);
                        if (1==operateDeveloperRobotAuthDeviceAuthDTO.getRobotToCloud()){
                            po.setDeviceUnique(map.get("deviceUnique"));
                        }else {
                            po.setDeviceUnique("");
                        }
                        po.setRobotId(robotId);
                        po.setStatus(DataStatusEnum.NORMAL.getType());
                        po.setCreateTime(new Date());
                        poList.add(po);
                    }
                }
            }
        }
        //情况2 机器人白名单授权为 1是 并且机器人设备授权为 0否
        if (operateDeveloperRobotAuthDeviceAuthDTO.getRobotWhiteListAuthStatus() != null && operateDeveloperRobotAuthDeviceAuthDTO.getRobotWhiteListAuthStatus() == 1
                && operateDeveloperRobotAuthDeviceAuthDTO.getRobotDeviceAuthStatus() != null && operateDeveloperRobotAuthDeviceAuthDTO.getRobotDeviceAuthStatus() == 0) {
            //获取项目下所有绑定的电梯
            List<OperateDeveloperAuthDeviceAuthListVO> listCloudElevator = iOperateDeveloperAuthDao.getDeviceListCloudElevator(SqlUtil.convertBeanToMapParams(operateDeveloperRobotAuthDeviceAuthDTO));

            //获取项目下所有绑定的门禁
            List<OperateDeveloperAuthDeviceAuthListVO> listCloudEntrance = iOperateDeveloperAuthDao.getDeviceListCloudEntrance(SqlUtil.convertBeanToMapParams(operateDeveloperRobotAuthDeviceAuthDTO));

            //处理门禁信息 云门禁云对接
            if (operateDeveloperRobotAuthDeviceAuthDTO.getRobotEntranceInfo() != null && !"".equals(operateDeveloperRobotAuthDeviceAuthDTO.getRobotEntranceInfo())) {
                List<Map<String, String>> listObjectFir = (List<Map<String, String>>) JSONArray.parse(operateDeveloperRobotAuthDeviceAuthDTO.getRobotEntranceInfo());
                for (Map<String, String> map : listObjectFir) {
                    String[] robotIds = map.get("robotId").split(",");//机器人白名单授权为 是
                    for (String robotId : robotIds) {
                        if (listCloudEntrance.size() == 0) {
                            RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                            po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                            po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                            po.setDeviceType(3);
                            po.setDeviceUnique("");
                            po.setRobotId(robotId);
                            po.setStatus(DataStatusEnum.NORMAL.getType());
                            po.setCreateTime(new Date());
                            poList.add(po);
                        } else {
                            for (OperateDeveloperAuthDeviceAuthListVO operateDeveloperAuthDeviceAuthListVO : listCloudEntrance) {
                                RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                                po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                                po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                                po.setDeviceType(3);
                                if (1==operateDeveloperRobotAuthDeviceAuthDTO.getRobotEntrance()){
                                    po.setDeviceUnique(operateDeveloperAuthDeviceAuthListVO.getDeviceUnique());
                                }else {
                                    po.setDeviceUnique("");
                                }
                                po.setRobotId(robotId);
                                po.setStatus(DataStatusEnum.NORMAL.getType());
                                po.setCreateTime(new Date());
                                poList.add(po);
                            }
                        }
                    }
                }
            }
            //处理机器人无感信息 多梯自动调度
            if (operateDeveloperRobotAuthDeviceAuthDTO.getRobotNoninductiveInfo() != null && !"".equals(operateDeveloperRobotAuthDeviceAuthDTO.getRobotNoninductiveInfo()) ) {
                List<Map<String, String>> NoninductivelistObjectFir = (List<Map<String, String>>) JSONArray.parse(operateDeveloperRobotAuthDeviceAuthDTO.getRobotNoninductiveInfo());
                for (Map<String, String> map : NoninductivelistObjectFir) {
                    String[] robotIds = map.get("robotId").split(",");//机器人白名单授权为 是
                    for (String robotId : robotIds) {
                        if (listCloudElevator.size() == 0) {
                            RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                            po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                            po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                            po.setDeviceType(1);
                            po.setDeviceUnique("");
                            po.setRobotId(robotId);
                            po.setStatus(DataStatusEnum.NORMAL.getType());
                            po.setCreateTime(new Date());
                            poList.add(po);
                        } else {
                            for (OperateDeveloperAuthDeviceAuthListVO operateDeveloperAuthDeviceAuthListVO : listCloudElevator) {
                                RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                                po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                                po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                                po.setDeviceType(1);
                                if (1==operateDeveloperRobotAuthDeviceAuthDTO.getRobotNoninductive()){
                                    po.setDeviceUnique(operateDeveloperAuthDeviceAuthListVO.getDeviceUnique());
                                }else {
                                    po.setDeviceUnique("");
                                }
                                po.setRobotId(robotId);
                                po.setStatus(DataStatusEnum.NORMAL.getType());
                                po.setCreateTime(new Date());
                                poList.add(po);
                            }
                        }

                    }
                }
            }
            ////机器人对云端信息 单梯云对接
            if (operateDeveloperRobotAuthDeviceAuthDTO.getRobotToCloudInfo() != null && !"".equals(operateDeveloperRobotAuthDeviceAuthDTO.getRobotToCloudInfo())) {
                List<Map<String, String>> RobotToCloudlistObjectFir = (List<Map<String, String>>) JSONArray.parse(operateDeveloperRobotAuthDeviceAuthDTO.getRobotToCloudInfo());
                for (Map<String, String> map : RobotToCloudlistObjectFir) {
                    String[] robotIds = map.get("robotId").split(",");//机器人白名单授权为 是
                    for (String robotId : robotIds) {
                        if (listCloudElevator.size() == 0) {
                            RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                            po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                            po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                            po.setDeviceType(2);
                            po.setDeviceUnique("");
                            po.setRobotId(robotId);
                            po.setStatus(DataStatusEnum.NORMAL.getType());
                            po.setCreateTime(new Date());
                            poList.add(po);
                        } else {
                            for (OperateDeveloperAuthDeviceAuthListVO operateDeveloperAuthDeviceAuthListVO : listCloudElevator) {
                                RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                                po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                                po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                                po.setDeviceType(2);
                                if (1==operateDeveloperRobotAuthDeviceAuthDTO.getRobotToCloud()){
                                    po.setDeviceUnique(operateDeveloperAuthDeviceAuthListVO.getDeviceUnique());
                                }else {
                                    po.setDeviceUnique("");
                                }
                                po.setRobotId(robotId);
                                po.setStatus(DataStatusEnum.NORMAL.getType());
                                po.setCreateTime(new Date());
                                poList.add(po);
                            }
                        }

                    }
                }
            }
        }
        //情况3 机器人白名单授权为 0否 并且机器人设备授权为 1是
        if (operateDeveloperRobotAuthDeviceAuthDTO.getRobotWhiteListAuthStatus() != null && operateDeveloperRobotAuthDeviceAuthDTO.getRobotWhiteListAuthStatus() == 0
                && operateDeveloperRobotAuthDeviceAuthDTO.getRobotDeviceAuthStatus() != null && operateDeveloperRobotAuthDeviceAuthDTO.getRobotDeviceAuthStatus() == 1) {
            //处理门禁信息 云门禁云对接
            if (operateDeveloperRobotAuthDeviceAuthDTO.getRobotEntranceInfo() != null && !"".equals(operateDeveloperRobotAuthDeviceAuthDTO.getRobotEntranceInfo())&& 1==operateDeveloperRobotAuthDeviceAuthDTO.getRobotEntrance()) {
                List<Map<String, String>> listObjectFir = (List<Map<String, String>>) JSONArray.parse(operateDeveloperRobotAuthDeviceAuthDTO.getRobotEntranceInfo());
                for (Map<String, String> map : listObjectFir) {
                    if (StringUtils.isBlank(map.get("robotId"))) {//机器人白名单授权为 否
                        RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                        po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                        po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                        po.setDeviceType(3);
                        po.setDeviceUnique(map.get("deviceUnique"));
                        po.setRobotId("");
                        po.setStatus(DataStatusEnum.NORMAL.getType());
                        po.setCreateTime(new Date());
                        poList.add(po);
                    }
                }
            }
            //处理机器人无感信息 多梯自动调度
            if (operateDeveloperRobotAuthDeviceAuthDTO.getRobotNoninductiveInfo() != null && !"".equals(operateDeveloperRobotAuthDeviceAuthDTO.getRobotNoninductiveInfo()) && 1==operateDeveloperRobotAuthDeviceAuthDTO.getRobotNoninductive()) {
                List<Map<String, String>> listObjectFir = (List<Map<String, String>>) JSONArray.parse(operateDeveloperRobotAuthDeviceAuthDTO.getRobotNoninductiveInfo());
                for (Map<String, String> map : listObjectFir) {
                    if (StringUtils.isBlank(map.get("robotId"))) {
                        RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                        po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                        po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                        po.setDeviceType(1);
                        po.setDeviceUnique(map.get("deviceUnique"));
                        po.setRobotId("");
                        po.setStatus(DataStatusEnum.NORMAL.getType());
                        po.setCreateTime(new Date());
                        poList.add(po);
                    }
                }
            }

            //机器人对云端信息 单梯云对接
            if (operateDeveloperRobotAuthDeviceAuthDTO.getRobotToCloudInfo() != null && !"".equals(operateDeveloperRobotAuthDeviceAuthDTO.getRobotToCloudInfo())&& 1==operateDeveloperRobotAuthDeviceAuthDTO.getRobotToCloud()) {
                List<Map<String, String>> listObjectFir = (List<Map<String, String>>) JSONArray.parse(operateDeveloperRobotAuthDeviceAuthDTO.getRobotToCloudInfo());
                for (Map<String, String> map : listObjectFir) {
                    if (StringUtils.isBlank(map.get("robotId"))) {
                        RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                        po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                        po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                        po.setDeviceType(2);
                        po.setDeviceUnique(map.get("deviceUnique"));
                        po.setRobotId("");
                        po.setStatus(DataStatusEnum.NORMAL.getType());
                        po.setCreateTime(new Date());
                        poList.add(po);
                    }
                }
            }
        }

        //情况4 机器人白名单授权为 0否 并且机器人设备授权为 0否
        if (operateDeveloperRobotAuthDeviceAuthDTO.getRobotWhiteListAuthStatus() != null && operateDeveloperRobotAuthDeviceAuthDTO.getRobotWhiteListAuthStatus() == 0
                && operateDeveloperRobotAuthDeviceAuthDTO.getRobotDeviceAuthStatus() != null && operateDeveloperRobotAuthDeviceAuthDTO.getRobotDeviceAuthStatus() == 0) {
            //获取项目下所有绑定的电梯
            List<OperateDeveloperAuthDeviceAuthListVO> listCloudElevator = iOperateDeveloperAuthDao.getDeviceListCloudElevator(SqlUtil.convertBeanToMapParams(operateDeveloperRobotAuthDeviceAuthDTO));

            //获取项目下所有绑定的门禁
            List<OperateDeveloperAuthDeviceAuthListVO> listCloudEntrance = iOperateDeveloperAuthDao.getDeviceListCloudEntrance(SqlUtil.convertBeanToMapParams(operateDeveloperRobotAuthDeviceAuthDTO));

            //处理门禁信息 云门禁云对接
            if (listCloudEntrance.size() == 0) {
                RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                po.setDeviceType(3);
                po.setDeviceUnique("");
                po.setRobotId("");
                po.setStatus(DataStatusEnum.NORMAL.getType());
                po.setCreateTime(new Date());
                poList.add(po);
            } else {
                for (OperateDeveloperAuthDeviceAuthListVO operateDeveloperAuthDeviceAuthListVO : listCloudEntrance) { //机器人设备授权为 1否
                    RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                    po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                    po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                    po.setDeviceType(3);
                    if (1==operateDeveloperRobotAuthDeviceAuthDTO.getRobotEntrance()){
                        po.setDeviceUnique(operateDeveloperAuthDeviceAuthListVO.getDeviceUnique());
                    }else {
                        po.setDeviceUnique("");
                    }
                    po.setRobotId("");
                    po.setStatus(DataStatusEnum.NORMAL.getType());
                    po.setCreateTime(new Date());
                    poList.add(po);
                }
            }

            //处理机器人无感信息 多梯自动调度
            if (listCloudElevator.size() == 0 ) {
                RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                po.setDeviceType(1);
                po.setDeviceUnique("");
                po.setRobotId("");
                po.setStatus(DataStatusEnum.NORMAL.getType());
                po.setCreateTime(new Date());
                poList.add(po);
            } else {
                for (OperateDeveloperAuthDeviceAuthListVO operateDeveloperAuthDeviceAuthListVO : listCloudElevator) { //机器人设备授权为 1否
                    RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                    po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                    po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                    po.setDeviceType(1);
                    if ( 1==operateDeveloperRobotAuthDeviceAuthDTO.getRobotNoninductive()){
                        po.setDeviceUnique(operateDeveloperAuthDeviceAuthListVO.getDeviceUnique());
                    }else {
                        po.setDeviceUnique("");
                    }
                    po.setRobotId("");
                    po.setStatus(DataStatusEnum.NORMAL.getType());
                    po.setCreateTime(new Date());
                    poList.add(po);
                }
            }

            ////机器人对云端信息 单梯云对接
            if (listCloudElevator.size() == 0 ) {
                RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                po.setDeviceType(2);
                po.setDeviceUnique("");
                po.setRobotId("");
                po.setStatus(DataStatusEnum.NORMAL.getType());
                po.setCreateTime(new Date());
                poList.add(po);
            } else {
                for (OperateDeveloperAuthDeviceAuthListVO operateDeveloperAuthDeviceAuthListVO : listCloudElevator) { //机器人设备授权为 1否
                    RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                    po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                    po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                    po.setDeviceType(2);
                    if (1==operateDeveloperRobotAuthDeviceAuthDTO.getRobotToCloud()){
                        po.setDeviceUnique(operateDeveloperAuthDeviceAuthListVO.getDeviceUnique());
                    }else {
                        po.setDeviceUnique("");
                    }
                    po.setRobotId("");
                    po.setStatus(DataStatusEnum.NORMAL.getType());
                    po.setCreateTime(new Date());
                    poList.add(po);
                }
            }

        }
        //批量保存机器人开发者数据
        if (poList.size() > 0) {
            iOperateDeveloperAuthDao.batchSaveDevelopInfo(poList);
        }
        //保存机器人开发者信息
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(operateDeveloperRobotAuthDeviceAuthDTO);
        return iOperateDeveloperAuthDao.updateRobotDevelopApi(params);


        //2.8.2版本以前的处理方式
       /* //处理门禁信息
        if (operateDeveloperRobotAuthDeviceAuthDTO.getRobotEntranceInfo() != null && !"".equals(operateDeveloperRobotAuthDeviceAuthDTO.getRobotEntranceInfo())) {
            List<Map<String, String>> listObjectFir = (List<Map<String, String>>) JSONArray.parse(operateDeveloperRobotAuthDeviceAuthDTO.getRobotEntranceInfo());
            for (Map<String, String> map : listObjectFir) {
                if (StringUtils.isBlank(map.get("robotId"))) {//机器人白名单授权为 否
                    RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                    po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                    po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                    po.setDeviceType(3);
                    po.setDeviceUnique(map.get("deviceUnique"));
                    po.setRobotId("");
                    po.setStatus(DataStatusEnum.NORMAL.getType());
                    po.setCreateTime(new Date());
                    poList.add(po);
                } else {
                    String[] robotIds = map.get("robotId").split(",");//机器人白名单授权为 是
                    for (String robotId : robotIds) {
                        RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                        po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                        po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                        po.setDeviceType(3);
                        po.setDeviceUnique(map.get("deviceUnique"));
                        po.setRobotId(robotId);
                        po.setStatus(DataStatusEnum.NORMAL.getType());
                        po.setCreateTime(new Date());
                        poList.add(po);
                    }
                }
            }
        }
        //处理机器人无感信息
        if (operateDeveloperRobotAuthDeviceAuthDTO.getRobotNoninductiveInfo() != null && !"".equals(operateDeveloperRobotAuthDeviceAuthDTO.getRobotNoninductiveInfo())) {
            List<Map<String, String>> listObjectFir = (List<Map<String, String>>) JSONArray.parse(operateDeveloperRobotAuthDeviceAuthDTO.getRobotNoninductiveInfo());
            for (Map<String, String> map : listObjectFir) {
                if (StringUtils.isBlank(map.get("robotId"))) {
                    RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                    po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                    po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                    po.setDeviceType(1);
                    po.setDeviceUnique(map.get("deviceUnique"));
                    po.setRobotId("");
                    po.setStatus(DataStatusEnum.NORMAL.getType());
                    po.setCreateTime(new Date());
                    poList.add(po);
                } else {
                    String[] robotIds = map.get("robotId").split(",");
                    for (String robotId : robotIds) {
                        RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                        po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                        po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                        po.setDeviceType(1);
                        po.setDeviceUnique(map.get("deviceUnique"));
                        po.setRobotId(robotId);
                        po.setStatus(DataStatusEnum.NORMAL.getType());
                        po.setCreateTime(new Date());
                        poList.add(po);
                    }
                }
            }
        }
        ////机器人对云端信息
        if (operateDeveloperRobotAuthDeviceAuthDTO.getRobotToCloudInfo() != null && !"".equals(operateDeveloperRobotAuthDeviceAuthDTO.getRobotToCloudInfo())) {
            List<Map<String, String>> listObjectFir = (List<Map<String, String>>) JSONArray.parse(operateDeveloperRobotAuthDeviceAuthDTO.getRobotToCloudInfo());
            for (Map<String, String> map : listObjectFir) {
                if (StringUtils.isBlank(map.get("robotId"))) {
                    RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                    po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                    po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                    po.setDeviceType(2);
                    po.setDeviceUnique(map.get("deviceUnique"));
                    po.setRobotId("");
                    po.setStatus(DataStatusEnum.NORMAL.getType());
                    po.setCreateTime(new Date());
                    poList.add(po);
                } else {
                    String[] robotIds = map.get("robotId").split(",");
                    for (String robotId : robotIds) {
                        RobotDeveloperAccountAuthPO po = new RobotDeveloperAccountAuthPO();
                        po.setUserId(operateDeveloperRobotAuthDeviceAuthDTO.getUserId());
                        po.setProjectId(operateDeveloperRobotAuthDeviceAuthDTO.getProjectId());
                        po.setDeviceType(2);
                        po.setDeviceUnique(map.get("deviceUnique"));
                        po.setRobotId(robotId);
                        po.setStatus(DataStatusEnum.NORMAL.getType());
                        po.setCreateTime(new Date());
                        poList.add(po);
                    }
                }
            }
        }
       //批量保存机器人开发者数据
        if (poList.size() > 0) {
            iOperateDeveloperAuthDao.batchSaveDevelopInfo(poList);
        }
        //保存机器人开发者信息
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(operateDeveloperRobotAuthDeviceAuthDTO);
        return iOperateDeveloperAuthDao.updateRobotDevelopApi(params);
*/
    }
}
